﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using Survey.Common;
using Survey.Common.Models.Services.HowTos;
using Survey.Common.Controllers.Services.HowTos;
using Survey.Common.Business.Services.HowTos;
using Survey.Entity.Services.HowTos;

namespace Survey.Mvc.Controllers.Services.HowTos
{
    /// <summary>
    /// All actions for SimpleEntity
    /// </summary>
    [CLSCompliant(false)]
    public class SimpleEntityController : StringEntityController<SimpleEntity, SimpleEntityFilter, ISimpleEntityModel, ISimpleEntityFacade>, ISimpleEntityController
    {
        #region OnList
        /// <summary>
        /// Provides columns for List view
        /// </summary>
        protected override IEnumerable<IItemToDisplay> LoadItemsToDisplay(SimpleEntityFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.ToDisplay(),
                    Items = new List<ICellDescription>
                    {
                        i.Cell(() => i.NaturalKey),
                        i.Cell(() => i.Description),
                        i.Cell(() => i.RealValue),
                        i.Cell(() => i.IsVisible),
                        i.Cell(() => i.ID, new CellDescription {Align = Align.right, Format = "N0"}),
                    }
                } as IItemToDisplay);
        }
        /// <summary>
        /// Provides columns for Excel
        /// </summary>
        protected override IEnumerable<IEnumerable<IExCell>> LoadExcelItemsToDisplay(SimpleEntityFilter filter)
        {
            return Facade.Find(filter)
                .Select(i => new List<IExCell>
                {
                    i.ExCell(() => i.ID),
                    i.ExCell(() => i.NaturalKey),
                    i.ExCell(() => i.Description),
                    i.ExCell(() => i.RealValue),
                    i.ExCell(() => i.IsVisible),
                    i.ExCell(() => i.NewID),
                });
        }
        #endregion OnList

        #region AutoComplete
        protected override void OnAutoCompleteList(SimpleEntityFilter filter, string term)
        {
            term = term ?? string.Empty;

            filter.NaturalKey = term + "*";
            filter.OrderBy["NaturalKey"] = true;

            Model.ListModel.ItemsToDisplay = Facade.Find(filter)
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.NaturalKey + " (" + i.Description + ")",
                } as IItemToDisplay);
        }
        #endregion AutoComplete

        #region OnBind
        #endregion OnBind

        #region OnBefore
        protected override void OnBeforeList()
        {
            base.OnBeforeList();
            Model.ListModel.ShowEditOnLeft = false;
        }
        #endregion OnBefore

        #region Actions
        #endregion Actions

        #region abstract ClearFilter
        /// <summary>
        ///     Pre-sets filtering filters
        /// </summary>
        protected override SimpleEntityFilter ClearFilter()
        {
            var filter = base.ClearFilter();
            // TODO set the MVC defaults e.g.: filter.OrderBy["ID"] = false;
            return filter;
        }
        #endregion ClearFilter

        #region properties
        public override string ControllerName { get { return Str.Controllers.SimpleEntity; } }

        // TODO:
        // e.g. IUserFacade if User instance is needed:
        // NOTE
        // if 'Project.config' file factoryProviderType is set:
        // value="ProjectBase.Data.Factory.IocWithProxyFactory, ProjectBase.Data
        // implementor of the 'IFacade' will be injected for you: LAZILY!

        // public virtual IUserFacade UserFacade { protected get; set; }

        #endregion properties
    }
}
